Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unchecked-type-assertion #889

Merged
merged 3 commits into from
Sep 17, 2023

Conversation

Dirk007
Copy link
Contributor

@Dirk007 Dirk007 commented Sep 6, 2023

Closes #888

Unchecked type assertions results can (and will) result in an unexpected panic and should be a lint.
This new rule covers this with configurable non-default option to ignore foo, _ := bar.(Baz) - cases where the ok value is ignored.

See tests.

@Dirk007 Dirk007 force-pushed the rule/unchecked_dynamic_cast branch from 652dc0a to de3b67b Compare September 6, 2023 09:52
@chavacava
Copy link
Collaborator

@denisvmedia
You can checkout @Dirk007 's branch and check how the linter behaves with your examples

@denisvmedia
Copy link
Collaborator

@chavacava I can, but I think this should be a part of the tests ;)

@Dirk007
Copy link
Contributor Author

Dirk007 commented Sep 11, 2023

@chavacava I can, but I think this should be a part of the tests ;)

I totally agree! Unfortunately I am a bit short of time this week but will work on the suggested ideas ASAP.

Copy link
Collaborator

@denisvmedia denisvmedia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all good from my POV (just a small issue in the test case). Good job 👍

@denisvmedia
Copy link
Collaborator

Just one thing came to my mind now, probably this is still left out (sorry, can't check it now): channel <- v.(int).

@Dirk007
Copy link
Contributor Author

Dirk007 commented Sep 14, 2023

Just one thing came to my mind now, probably this is still left out (sorry, can't check it now): channel <- v.(int).

good idea.. let me write a test 👍


```go
foo, _ := bar(.*Baz).
// ^
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be interesting to list all the cases the rule is capable to detect (switch, channel, ...)

u.Lock()
defer u.Unlock()

if len(arguments) == 0 {
Copy link
Collaborator

@chavacava chavacava Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a configured field to the rule's struct to trace the rule's configuration was already processed and then quit ASAP once the rule was configured a first time.

Suggested change
if len(arguments) == 0 {
if len(arguments) == 0 || u.configured {

@chavacava chavacava merged commit 95acb88 into mgechev:master Sep 17, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Rule for dangerous type casts
3 participants